﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using easysql;
namespace smsclient
{
    public enum RSMSTYPE
    {
        发送成功,
        发送失败,
        接收信息
    }
    public class ESms{
        public override string ToString()
        {
            return Mbno + "," + DT +","+V2+ ":" + Msg;
        }


        public String Mbno{get;set;}
        public String Msg{get;set;}
        public DateTime DT{get;set;}//发送时间
        /// <summary>
        /// 短信标识
        /// </summary>
        public String V2 { get; set; }//
    }
    public class RSms{

        public override string ToString()
        {
            return Mbno + "," + DT + "," + V3 +","+BadWhy+ ":" + Msg;
        }


        public String Mbno { get; set; }
        public String Msg { get; set; }
        public DateTime DT { get; set; }
        /// <summary>
        /// 对应发送短信的id
        /// </summary>
        public int V3 { get; set; }
        public RSMSTYPE Type { get; set; }

        /// <summary>
        /// 短信发送失败原因
        /// </summary>
        public String BadWhy { get; set; }
    }

    public delegate void DGRSms(RSms sms);
    public class SmsOpertor
    {
        public DGRSms OnRec;

        private BaseDBHelper dh;
        public SmsOpertor(BaseDBHelper dh)
        {
            this.dh = dh;
        }

        /// <summary>
        /// 发送短信,返回短信编号
        /// </summary>
        /// <param name="sms"></param>
        /// <returns></returns>
        public int SendSms(ESms sms)
        {
            Sms_OutBoxModel outModel = new Sms_OutBoxModel();
            
            outModel.Mbno = sms.Mbno;
            outModel.Msg = sms.Msg;
            outModel.SendTime = sms.DT;

            dh.Add_AutoSetId<Sms_OutBoxModel>("outbox", outModel);
            return outModel.Id;
        }
        /// <summary>
        /// 通过短信编号查询短信是否发送成功或失败,如果有成功或失败将触发OnRec事件
        /// </summary>
        /// <param name="ids"></param>
        public void RefreshYesOrNo(object[] ids)
        {
            var SendedOutBoxList = dh.Query<Sms_SendedOutBoxModel>("SendedOutBox", null, Restrain.In("V3", ids));
            var BadOutBoxList = dh.Query<Sms_BadOutBoxModel>("badoutbox", null, Restrain.In("V3", ids));

            foreach (var model in SendedOutBoxList)
            {
                var rsms = new RSms();
                rsms.Mbno = model.Mbno;
                rsms.Msg = model.Msg;
                rsms.DT = model.SendTime;
                rsms.Type = RSMSTYPE.发送成功;
                rsms.V3 = int.Parse(model.V3);

                if (OnRec != null)
                {
                    OnRec(rsms);
                }
            }
            foreach (var model in BadOutBoxList)
            {
                var rsms = new RSms();
                rsms.Mbno = model.Mbno;
                rsms.Msg = model.Msg;
                rsms.DT = model.SendTime;
                rsms.Type = RSMSTYPE.发送失败;
                rsms.V3 = int.Parse(model.V3);
                if (OnRec != null)
                {
                    OnRec(rsms);
                }
            }

        }

    }
}
